package csu.rjxyg.action.courseAction;

import java.util.ArrayList;
import java.util.List;

import csu.rjxyg.action.BaseAction;
import csu.rjxyg.action.constant.CourseConstant;
import csu.rjxyg.action.constant.UserConstant;
import csu.rjxyg.bean.core.BaseBean;
import csu.rjxyg.bean.core.BaseObject;
import csu.rjxyg.factory.core.UtilFactory;
import csu.rjxyg.service.BaseCourseService;
import csu.rjxyg.service.BaseUserService;
import csu.rjxyg.util.LogUtil;

public class InsertCourseInfoAction extends BaseAction {
	private LogUtil log;
	
	private String courseId;
	private String courseName;
	private String courseTeacher;
	private String courseTime;
	private String courseRoom;
	
	
	public String getCourseId() {
		return courseId;
	}

	public void setCourseId(String courseId) {
		this.courseId = courseId;
	}

	public String getCourseName() {
		return courseName;
	}

	public void setCourseName(String courseName) {
		this.courseName = courseName;
	}

	public String getCourseTeacher() {
		return courseTeacher;
	}

	public void setCourseTeacher(String courseTeacher) {
		this.courseTeacher = courseTeacher;
	}

	public String getCourseTime() {
		return courseTime;
	}

	public void setCourseTime(String courseTime) {
		this.courseTime = courseTime;
	}

	public String getCourseRoom() {
		return courseRoom;
	}

	public void setCourseRoom(String courseRoom) {
		this.courseRoom = courseRoom;
	}

	public InsertCourseInfoAction() {
		log = UtilFactory.getLogUtil(getClass());
	}
	
	public void insertCourseBean() {
		String name = null;
		try {

			BaseCourseService courseService = (BaseCourseService) super.factory.getBean(BaseAction.COURSE_SERVICE);
			BaseUserService userService = (BaseUserService) super.factory.getBean(BaseAction.USER_SERVICE);
			BaseObject object = (BaseObject) this.getSession().get(UserConstant.USER_SESSION);
			name = (String) object.get(UserConstant.USER_ID);

			BaseObject temp = (BaseObject) factory.getBean(BaseBean.BEAN_OBJECT);
			BaseObject teacher = null;
			
			
			
			if (courseService.getBeanInfoById(courseId) != null) {
				//temp.set(ERROR, "课程编号重复，请重新输入！");
				throw new Exception("课程编号重复，请重新输入！");
			} else if ((teacher = userService.getBeanInfoById(courseTeacher)) == null) {
				throw new Exception("教师编号输入有误：" + courseTeacher);
			}
			if( name.equals(courseTeacher) ){
				teacher = object;
			}
			temp.set(CourseConstant.COURSE_ID, courseId);
			temp.set(CourseConstant.COURSE_NAME, courseName);
			temp.set(CourseConstant.COURSE_TEACHER, courseTeacher);
			temp.set(CourseConstant.COURSE_TIME, courseTime);
			temp.set(CourseConstant.COURSE_ROOM, courseRoom);
			
			List courseList = (List) teacher.get(UserConstant.TEACH_COURSE);
			if( courseList  == null ){
				courseList = new ArrayList();
			}
			courseList.add(courseId);
			teacher.set(UserConstant.TEACH_COURSE, courseList);
			if (courseService.insertBean(temp) && userService.updateUserBean(courseTeacher, teacher)) {
				log.info(name + "新建课程信息成功，信息如下: " + temp.getBean());
			}else{
				log.info(name + "新建课程信息失败");
			}
		} catch (Exception e) {
			log.info(name + "新建课程信息失败");
			e.printStackTrace();
			log.info(e.getMessage());
		}
	}
	
}
